package com.example.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.app.entity.Collect;
import com.example.app.entity.Consumer;
import com.example.app.entity.Song;
import com.example.app.service.ICollectService;
import com.example.app.service.IConsumerService;
import com.example.app.service.ISongService;
import com.example.common.config.ConfigProperties;
import com.example.common.config.TransWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
public class PersonalController {
    @Autowired
    private ConfigProperties configProperties;
    @Autowired
    private ICollectService collectService;
    @Autowired
    private ISongService songService;
    @Autowired
    private IConsumerService consumerService;

    @GetMapping("/user/record")
    public Map<String, Object> login(HttpServletRequest request){

        String uid = request.getParameter("uid");

        Consumer one = consumerService.getById(Integer.parseInt(uid));

        Map<String, Object> result = null;

        if(one != null){
            QueryWrapper<Collect> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id", one.getId());
            List<Collect> list = collectService.list(wrapper);
            if(!CollectionUtils.isEmpty(list)){
                List<Integer> songIds = list.stream().map(Collect::getSongId).collect(Collectors.toList());

                List<Song> songs = songService.listByIds(songIds);
                songs.forEach(e -> {
                    e.getAl().setPicUrl(configProperties.getNginxBasePath() + e.getPic());
                });

                List<PlayList> collect = songs.stream().map(PlayList::new).collect(Collectors.toList());

                result = TransWrapper.wrap("allData", collect);

                result.put("code", 200);
            }
        }
        return result;
    }

    public static class PlayList{
        private Song song;

        public PlayList(Song song) {
            this.song = song;
        }

        public Song getSong() {
            return song;
        }

        public void setSong(Song song) {
            this.song = song;
        }
    }

}
